package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.vo.LeadVo;
import com.opgea.crm.domain.entities.Lead;
import com.opgea.crm.domain.entities.Leadphones;
import com.opgea.crm.domain.modal.QualifierModel;
import com.opgea.crm.repository.LeadRepo;

@Service
public class CustomerServiceImpl implements CustomerService{

	@Autowired
	LeadRepo leadRepo;
	
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void create(Lead lead) {
		lead.setLeadStatus(ApplicationConstant.LeadStatusQualifier.Customer);
    	lead.setActiveStatus(ApplicationConstant.ActiveQualifier.Active);
		leadRepo.create(lead);
	}
	
	@Transactional
	public void update(Lead lead) {
		leadRepo.update(lead);
	}	

	public void remove(long id) {
		// TODO Auto-generated method stub
	}

	public Lead find(long id) {
		return leadRepo.find(id);
	}

	public List<LeadVo> findAll() {
		
		List<LeadVo> list=new ArrayList<LeadVo>();
    	for(Lead data: leadRepo.findAll())
    	{
    		LeadVo leadVo=new LeadVo();
    		leadVo.setId(data.getId());
    		leadVo.setFirstName(data.getFirstName());
    		leadVo.setMiddleInitial(data.getMiddleInitial());
    		leadVo.setLastName(data.getLastName());
    		leadVo.setTitle(data.getTitle());
    		if(data.getLeademail() !=null){
     		   leadVo.setEmail(data.getLeademail().getAddress());
     		}
     		if(data.getCompany() != null){
     		   leadVo.setCompanyName(data.getCompany().getName());
     		}
     		
     		List<Leadphones> phones = new ArrayList<Leadphones>(data.getLeadphonesCollection());
     		if(phones.size() > 0){
     		      leadVo.setPhone1(phones.get(0).getPhoneNo());
     		}
     		if(phones.size() > 1){
     		      leadVo.setPhone2(phones.get(1).getPhoneNo());
     		}
    		list.add(leadVo);
    		//System.out.println("Lead "+list.indexOf(leadVo)+": "+leadVo);
    	}
    	return list;
	}
	
	public List<LeadVo> findDynamically(String criteria, String value) {
		List<LeadVo> list=new ArrayList<LeadVo>();
    	for(Lead data: leadRepo.findDynamically(criteria, value, ApplicationConstant.LeadStatusQualifier.Customer))
    	{
    		LeadVo leadVo=new LeadVo();
    		leadVo.setId(data.getId());
    		leadVo.setFirstName(data.getFirstName());
    		leadVo.setMiddleInitial(data.getMiddleInitial());
    		leadVo.setLastName(data.getLastName());
    		leadVo.setTitle(data.getTitle());
    		if(data.getLeadprofile()!= null){
    			leadVo.setGrade(data.getLeadprofile().getGrade().getFieldValue());
    		}
    		if(data.getLeadprofile()!= null){
    			leadVo.setStage(data.getLeadprofile().getStage().getFieldValue());
    		}
    		if(data.getLeadprofile()!= null){
    			leadVo.setAssignedTo(data.getLeadprofile().getEmployee().getFirstName());
    		}
    		
    		if(data.getLeademail() !=null){
    		   leadVo.setEmail(data.getLeademail().getAddress());
    		}
    		if(data.getCompany() != null){
    		   leadVo.setCompanyName(data.getCompany().getName());
    		}
    		
    		List<Leadphones> phones = new ArrayList<Leadphones>(data.getLeadphonesCollection());
    		if(phones.size() > 0){
    		      leadVo.setPhone1(phones.get(0).getPhoneNo());
    		}
    		if(phones.size() > 1){
    		      leadVo.setPhone2(phones.get(1).getPhoneNo());
    		}
    		
    		System.out.println(phones.get(0).getPhoneNo());
    		list.add(leadVo);
    		System.out.println("Lead "+list.indexOf(leadVo)+": "+leadVo);
    	}
    	return list;
	}
	
	public List<QualifierModel> findAllLeadName(){
		
		List<Lead> leads = leadRepo.findAll();
		List<QualifierModel> leadNameList = new ArrayList<QualifierModel>();
		
		for(Lead lead : leads)
		{
			leadNameList.add(new QualifierModel(lead.getId(), lead.getFirstName()));
			System.out.println("Lead Name : "+ lead.getFirstName());
		}
		return leadNameList;
	}

	public List<Lead> findAllByCompanyId(long companyId) {
		return leadRepo.findAllByCompanyId(companyId);
	}

	public List<QualifierModel> findAllLeadNameByCompanyId(long companyId) {
		List<Lead> leads = leadRepo.findAllByCompanyId(companyId);
		List<QualifierModel> leadNameList = new ArrayList<QualifierModel>();
		
		for(Lead lead : leads)
		{
			leadNameList.add(new QualifierModel(lead.getId(), lead.getFirstName()));
			System.out.println("Assign To : id : "+lead.getId()+", Value :  "+lead.getFirstName());
		}
		return leadNameList;
	}
}
